package moonhy.c2dm_client;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import moonhy.c2dm_client.dw.MemberData;
import moonhy.c2dm_client.dw.NoticeData;
import moonhy.c2dm_client.util.JSONSharedPreferences;
import moonhy.c2dm_client.util.httpConnectionManager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;

public class MainActivity extends Activity {
	NoticeData tmp_nd = new NoticeData();
	//public final static String AUTH		= "authentication";
	//public final static String PHONE	= "phoneno";
	ArrayList<MemberData> md_array = new ArrayList<MemberData>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		Calendar today = Calendar.getInstance();
	    String calCurrent;

	    calCurrent = String.format("%02d", today.get(Calendar.MONTH) + 1);
	    
	    LinearLayout BirthList = (LinearLayout)findViewById(R.id.layoutBirthList);
        
    	int j = 0;
    	LinearLayout BirthListSub = new LinearLayout(this);
		try {
			JSONArray json_result = JSONSharedPreferences.loadJSONArray(this, "gline", "memberInfo");
			
			Log.d("C2DM", "생일자 조회: " + json_result.length());
			
			for (int i = 0; i < json_result.length(); i++) {
				JSONObject jsonObject = json_result.getJSONObject(i);
				MemberData tmp_nd = new MemberData();
				tmp_nd.setMemberName(jsonObject.getString("mem_name"));
				tmp_nd.setMemberPhone(jsonObject.getString("mem_phone"));
				tmp_nd.setMemberEmail(jsonObject.getString("mem_email"));
				tmp_nd.setMemberBirth(jsonObject.getString("mem_birth"));
				md_array.add(i, tmp_nd);
				
				//Log.d("C2DM", jsonObject.getString("mem_birth").substring(5,7) + " == " + calCurrent);
				
				if(jsonObject.getString("mem_birth").substring(5,7).equals(calCurrent)){
					
					if(j % 2 == 0){
						BirthListSub = new LinearLayout(this);
						BirthListSub.setOrientation(LinearLayout.HORIZONTAL);
					}
					
					TextView birthDateView = new TextView(this);
					birthDateView.setText(jsonObject.getString("mem_name") + "\n" + jsonObject.getString("mem_birth"));
					birthDateView.setTextColor(0xFFFFFFFF);
					birthDateView.setBackgroundColor(0xFF787974);
					birthDateView.setTextSize(15);
					birthDateView.setPadding(10, 10, 10, 10);
		    		
		    		//regDateView.setBackgroundColor(0x99313029);
		    		//regDateView.setPadding(20, 50, 50, 15);
		    		//regDateView.setBackgroundDrawable(shape);
		    		
		    		LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0.5f);
		    		params.setMargins(1, 0, 1, 0);
		    		BirthListSub.addView(birthDateView, params);
		    		
		    		LayoutParams paramsSub = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		    		paramsSub.setMargins(0, 0, 0, 2);
		    		
		    		if(j % 2 == 0){
		    			BirthList.addView(BirthListSub, paramsSub);
					}
		    		j++;
				}
			}
			
			if( j == 0){
				TextView birthDateView = new TextView(this);
				birthDateView.setText("이달의 생일자가 없습니다.");
				birthDateView.setTextColor(0xFFFFFFFF);
				birthDateView.setBackgroundColor(0xFF787974);
				birthDateView.setTextSize(15);
				birthDateView.setPadding(10, 10, 10, 10);
				
				LayoutParams paramsSub = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
				BirthList.addView(birthDateView, paramsSub);
			}
				
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	private void setData(Context context){
		//String readNotice = readJSON("http://moonhy.mireene.com/kline/notice/notice_list.php");
		try {
			httpConnectionManager hcm = new httpConnectionManager(context);
			
			JSONObject json			= new JSONObject(hcm.readJSON(getString(R.string.SERVER_URL) + "notice/notice_list.php", null));
			JSONObject json_header	= json.getJSONObject("header");
			JSONArray json_result	= json.getJSONArray("data_result");
			JSONObject jsonObject	= json_result.getJSONObject(0);

			tmp_nd.setSeqno(jsonObject.getInt("seqno"));
			tmp_nd.setTitle(jsonObject.getString("title"));
			tmp_nd.setContent(jsonObject.getString("content"));
			tmp_nd.setReg_date(jsonObject.getString("reg_date"));
			tmp_nd.setReg_Check(jsonObject.getString("reg_check"));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private class initPage extends AsyncTask<Void, Void, Void> implements DialogInterface.OnCancelListener{
		private ProgressDialog dialog;
		private Context context;
		
		private initPage(Context context){
			this.context	= context;
		}

		protected void onPreExecute(){
			dialog = ProgressDialog.show(context, "", "Loading. Please wait...", true);
		}

		protected Void doInBackground(Void... unused){
			setData(context);
    		return null;
        }

        protected void onPostExecute(Void unused)
        {
            dialog.dismiss();
            //TextView mainNoticeViewHeader = (TextView)findViewById(R.id.mainNoticeViewHeader);
            //mainNoticeViewHeader.setText("최신공지사항: [" + tmp_nd.getReg_date() + "]" + tmp_nd.getTitle());
            
            //TextView mainNoticeView = (TextView)findViewById(R.id.mainNoticeView);
            //mainNoticeView.setText(tmp_nd.getContent());
        }

        public void onCancel(DialogInterface dialog)
        {
        	cancel(true);
        	dialog.dismiss();
        }
	}
}
